new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQL_SerialNumber: '',//表名
                SQL_Name: '',//是否为子表
                SQL_Statement: '',
            },
            rules: {
                SQL_SerialNumber: [{ required: true, message: '请输入SQL标识', trigger: 'blur' }],
                SQL_Statement: [{ required: true, message: '请输入SQL语句', trigger: 'blur' }],
                SQL_Name: [{ required: true, message: '请输入SQL名称', trigger: 'blur' }],
            },
            loading: false,
        }
    },
    mounted() {
        const SQL_SerialNumber = sessionStorage.getItem("SQL_SerialNumber")
        console.log(SQL_SerialNumber);
        this.getData(SQL_SerialNumber);
    },
    methods: {
        getData(SQL_SerialNumber){
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: 'POST',
                data: { 
                    Data: "base_sql_list" ,
                    Condition: "SQL_SerialNumber='" + SQL_SerialNumber + "'"
                },
                success: function (data) {
                    if (data.length == 1) {
                        let str = decodeURIComponent(escape(atob(data[0].SQL_Statement)))
                        that.form = data[0];
                        that.form.SQL_Statement = str;
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);//config/config.js 响应预检
                },
                dataType: "json"
            });
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true;
                    let obj = deepClone(that.form);
                    obj.SQL_Statement = window.btoa(unescape(encodeURIComponent(obj.SQL_Statement)));
                    $.ajax({
                        headers:requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Modify: { base_sql_list: [obj] } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);//config/config.js 响应预检
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})